<?php
namespace app\api\controller;
use think\Controller;
use think\Db;

class Getinfo extends Controller {
	//通过手机号获取 vipCardNo 会员卡号
    public function getVipCardNo(){
		$apilog = new Common();
        $phone = input('phone',session('userinfo.Mobile'));
//      return $phone;
        try{
        	$sign = json_decode($this->connectMysql());
            $guid = $sign->workGuid;
            $key = $sign->key;
            // 输入报文中的随机整数，目的是为了使报文的长度不固定，从而影响加密结果以混淆报文
            $random = rand ( 0, 99 );
            // 输入报文格式
            $body = '<?xml version="1.0" encoding="UTF-8"?>
					<InputParameter>
						<Function>3</Function>
						<RandomNo>'.$random.'</RandomNo>
						<CardNo>'.$phone.'</CardNo>
						<CardNoType>2</CardNoType>
						<AccType>99</AccType>
						<OrgCode>1008</OrgCode>
						<YwType>101COMM</YwType>
					</InputParameter>';	
			$inParam = $body;
            require_once("Java.inc");
            $des = new \Java('RSAEncrpt.DES3');
            $body = $des->encrypt($body,$key);
            $para = '<?xml version="1.0" encoding="UTF-8"?>
            <InputParameter>
                <Head><ClientCode>001</ClientCode></Head>
                <Body>'.$body.'</Body>
            </InputParameter>';
            $client = new \SoapClient("http://60.30.83.101:1144/HisComSvr/HsCRMWebSrv.dll/wsdl/IHsCRMWebSrv?wsdl");
            $output = $client->IWsPosCommOperate ( '3', $para );
            if ($output->ReturnCode == 0){
                $rstXml = $des->decrypt($output->OutputPara,$key);
                $rst = xmlToArray($rstXml);
				$apilog->dataFormat($inParam,$rst,'/api/Getinfo/getVipCardNo',$rstXml,"xml","获取会员卡号接口成功");
                session("cardNo",$rst['VipCardNo']);
                return $rst['VipCardNo'];
            } else {
            	$errMsg = iconv("GBK","UTF-8", base64_decode($output->RtnMsg));
				$apilog->dataFormat($inParam,$errMsg,'/api/Getinfo/getVipCardNo',$errMsg,'xml','获取会员卡号接口失败');
            	return '0';
//              echo iconv("GBK","UTF-8", base64_decode($output->RtnMsg));
            }
        }catch (\Exception $e){
			$apilog->dataFormat($phone,$e,'/api/Getinfo/getVipCardNo',$e,"xml","获取会员卡号接口失败");
            return '无';
        }
    }
    //获取消费积分的接口type1为停车，0为消费
    public function getScore(){
		$apilog = new Common();
        $card = input('card');
        $type = input('type',0);
        try{
          	$sign = json_decode($this->connectMysql());
            $guid = $sign->workGuid;
            $key = $sign->key;
            // 输入报文中的随机整数，目的是为了使报文的长度不固定，从而影响加密结果以混淆报文
            $random = rand ( 0, 99 );
            // 输入报文格式
            $body = '<?xml version="1.0" encoding="UTF-8"?>   
                    <InputParameter>
                        <Function>5</Function>
                        <Random>'.$random.'</Random>
                        <WorkGuid>'.$guid.'</WorkGuid>
                        <QueryType>'.$type.'</QueryType>
                        <VipCardNo>'.$card.'</VipCardNo>
                    </InputParameter>';
			$inParam = $body;
            require_once("Java.inc");
            $des = new \Java('RSAEncrpt.DES3');
            $body = $des->encrypt($body,$key);
            $para = '<?xml version="1.0" encoding="UTF-8"?>
            <InputParameter>
                <Head><ClientCode>001</ClientCode></Head>
                <Body>'.$body.'</Body>
            </InputParameter>';
            $client = new \SoapClient("http://60.30.83.101:1144/HisComSvr/HsCRMWebSrv.dll/wsdl/IHsCRMWebSrv?wsdl");
            $output = $client->IWsPosCommOperate ( '1002', $para );
            if ($output->ReturnCode == 0)
            {
                $rstXml = $des->decrypt($output->OutputPara,$key);
                $rst = xmlToArray($rstXml);
                $rst = $rst['Data']['ROW'];
                $PrjCode = $type==1?'2003':'2001';
                //获取PrjCode为2003的停车积分或2001的消费积分
				$apilog->dataFormat($inParam,$rst,'/api/Getinfo/getScore',$rstXml,"xml","获取消费积分接口成功");
				if (count($rst) == count($rst, 1)) {
                	session("myScore",$rst['LfJfTotal']);
					return $rst['LfJfTotal'];
				} else {
					$k = array_search($PrjCode, array_column($rst, 'PrjCode'));
//					session('LfJfTotal',$rst[$k]['LfJfTotal']);
					return $rst[$k]['LfJfTotal'];
				}
            } else {
				$apilog->dataFormat($inParam,$output,'/api/Getinfo/getScore',$output,"xml","获取消费积分接口失败");
				return '0';
            }
        }catch (\Exception $e){
			$apilog->dataFormat($card,$e,'/api/Getinfo/getScore',$e,"xml","获取消费积分接口失败");
            return '0';
        }
    }
    
    //获取活动列表
    public function getActions(){
    	//手机号
		$mobile = input('mobile');
   		$actionList = Db::name('activity')
        	->alias('a')
   			->where('a.status',1)
            ->select();
        $where = [];
        $begin = date('Y-m-d 00:00:00');
        $where[] = search_time('gettime',$begin,'');
        $actions = [];
        foreach($actionList as $key => $val){
        	$exchange = Db::name('exchange')
        		->where('action_id = '.$val['id'])
        		->where('mobile',$mobile)
        		//->where('status',1)
        		->count();
        	$val['joined_num'] = $exchange;
        	$exchangeToday = Db::name('exchange')
        		->where('action_id = '.$val['id'])
        		->where('mobile',$mobile)
        		->where($where)
        		//->where('status',1)
        		->count();
        	$val['joined_today'] = $exchangeToday;
        	$actions[] = $val;
        }
//      $actions = Db::name('activity a')
// 			->where('dq_exchange e','a.id = a.action_id')
// 			->where('a.status',1)
// 			->field('a.*,count(e.action_id) as joinnum,e.username')
// 			->group('e.action_id')
//          ->select();
//      dump($actions);
//      dump($data);
//      return json_encode($actions);
        $this->success('获取成功','/api/Getinfo/getActions',$actions);
    }
    //根据活动id获取轮播图列表、券列表
    public function getActionInfo(){
    	//手机号
		$mobile = input('mobile');
    	$action_id = input("actionId");
    	$actionInfo = Db::name('activity')
 			->where('id','=',$action_id)
            ->find();
 		$banners = Db::name('banner')
 			->where('action_id','=',$action_id)
 			->where('status',1)
            ->select();
        $couponList = Db::name('coupon')
 			->where('action_id','=',$action_id)
 			->where('status',1)
            ->select();
        $where = [];
        $begin = date('Y-m-d 00:00:00');
        $where[] = search_time('gettime',$begin,'');
        $coupons = [];
        foreach($couponList as $key => $val){
        	$exchange = Db::name('exchange')
        		->where('coupon_id = '.$val['id'])
        		->where('mobile',$mobile)
        		->where('status',1)
        		->count();
        	$val['got_num'] = $exchange;
        	$exchangeToday = Db::name('exchange')
        		->where('coupon_id = '.$val['id'])
        		->where('mobile',$mobile)
        		->where($where)
        		->where('status',1)
        		->count();
        	$val['got_today'] = $exchangeToday;
        	$coupons[] = $val;
        }
        
        $data = [
        	'actionInfo' => $actionInfo,
        	'bannerList' => $banners,
        	'couponList' => $coupons,
        	'score' => session("myScore"),
        	'cardNo' => session("cardNo"),
//      	'userInfo' => session('userinfo')
        ];
//      return json_encode($data);
        $this->success('获取成功','/api/Getinfo/getActionInfo',$data);
    }
    //跨数据库读数据
    public function connectMysql(){
    	$result = Db::connect('db_saas');
    	$sign = $result->table('sylw_zapi1_actionlist')->where("id","=",1)->find();
		$data = [
			'guid' => $sign['workGuid'],
            'key' => $sign['key']
		];
		return json_encode($sign);
    }
}
